package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.entity.Comment;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.VideoComment;
import com.example.xpvideo.entity.req.ReplyList;
import com.example.xpvideo.entity.req.SecondReplyList;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.res.GoodsCommentsRes;
import com.example.xpvideo.entity.res.PhpSecondComment;
import com.example.xpvideo.entity.res.PhpVideoCommentRes;
import com.example.xpvideo.entity.res.SecondComments;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.PhpVideoCommentMapper;
import com.example.xpvideo.mapper.VideoCommentMapper;
import com.example.xpvideo.service.PhpVideoCommentService;
import com.example.xpvideo.service.PhpVideoService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : 洪俊新
 * @date : 2021/9/14 16:33
 * @description:
 */
@Service
public class PhpVideoCommentServiceImpl implements PhpVideoCommentService {
    @Autowired
    private PhpVideoCommentMapper phpVideoCommentMapper;
    @Autowired
    private VideoCommentMapper videoCommentMapper;
    @Autowired
    private TimeService timeService;

    @Override
    public ResponseResult<PageResult<List<PhpVideoCommentRes>>> replyList(ReplyList replyList) throws Exception {
        Integer page = replyList.getPage();
        Integer size = replyList.getSize();
        String targetId = replyList.getTargetId();
        Integer type = replyList.getType();
        if (ObjectUtils.isEmpty(page)) {
            page = 1;
        }
        if (ObjectUtils.isEmpty(size)) {
            size = 10;
        }

        PageInfo<PhpVideoCommentRes> objectPageInfo = PageHelper.startPage(page, size)
                .doSelectPageInfo(() -> phpVideoCommentMapper.PhpcommentListByTarId(Wrappers.<VideoComment>query().eq("c.target_id",
                                targetId)
                        .eq("type",type)
                        .eq("c.comment_level", 1)
                        .eq("c.deleted",0)
                        .orderByAsc( "c.create_time")));

        List<PhpVideoCommentRes> commentListByTarIdList = objectPageInfo.getList();
        //当前页
        int pageNum = objectPageInfo.getPageNum();
        //总页数
        int pages = objectPageInfo.getPages();
        //当前页展示的数量
        int pageSize = objectPageInfo.getSize();
        //查询总数
        long count = objectPageInfo.getTotal();
        PageResult<List<PhpVideoCommentRes
                >> listPageResult = new PageResult<>(count, pages, pageSize, pageNum,
                commentListByTarIdList);

        return ResponseResult.responseResult(0, listPageResult);
    }

    @Override
    public ResponseResult<PageResult<List<PhpSecondComment>>> secondReplyList(SecondReplyList secondReplyList) throws Exception {
        Integer page = secondReplyList.getPage();
        Integer size = secondReplyList.getSize();
        String parentCommentId = secondReplyList.getParentCommentId();

        if (ObjectUtils.isEmpty(page)) {
            page = 1;
        }
        if (ObjectUtils.isEmpty(size)) {
            size = 10;
        }
        PageInfo<PhpSecondComment> objectPageInfo = PageHelper.startPage(page, size)
                .doSelectPageInfo(() -> phpVideoCommentMapper.PhpsecondCommentList(Wrappers.<VideoComment>query()
                        .eq("c.parent_comment_id", parentCommentId)
                        .eq("c.comment_level", 2)
                        .eq("c.status", 10)
                        .eq("c.deleted",0)
                        .orderByAsc("c.create_time")));

        List<PhpSecondComment> commentListByTarIdList = objectPageInfo.getList();
        //当前页
        int pageNum = objectPageInfo.getPageNum();
        //总页数
        int pages = objectPageInfo.getPages();
        //当前页展示的数量
        int pageSize = objectPageInfo.getSize();
        //查询总数
        long count = objectPageInfo.getTotal();
        PageResult<List<PhpSecondComment>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum,
                commentListByTarIdList);

        return ResponseResult.responseResult(0, listPageResult);
    }

    @Override
    public ResponseResult<Object> deleteComment(UpdateReq updateReq) throws ErrorException {
        int res = videoCommentMapper.update(null, Wrappers.<VideoComment>update()
                .set("deleted",1)
                        .set("update_time",timeService.getTime())
                .eq("id",updateReq.getId()));

        if(res > 0){
            return ResponseResult.responseResult(0, "删除评论成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20001);
    }
}
